package com.hoshiicloud.saleorder.controller.customer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.saleorder.param.customer.GoodsCollectRecordAddParam;
import com.hoshiicloud.saleorder.param.customer.GoodsCollectRecordQueryParam;
import com.hoshiicloud.saleorder.service.customer.IGoodsCollectRecordService;
import com.hoshiicloud.user.entity.customer.GoodsCollectRecord;
import com.hoshiicloud.user.vo.customer.GoodsCollectRecordDetailVO;
import com.hoshiicloud.user.vo.customer.GoodsCollectRecordVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户商品收藏记录 前端控制器 收藏不需要修改操作
 * </p>
 *
 * @author Mr.Wu
 * @since 2019-04-28
 */
@Api(value = "前台用户操作", tags = {"用户商品收藏记录管理"})
@Slf4j
@Validated
@RestController
@RequestMapping("/customer/goods-collect-record")
public class GoodsCollectRecordController {

    @Autowired
    private IGoodsCollectRecordService goodsCollectRecordService;

    @ApiOperation(value = "新增用户商品收藏记录", notes = "")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Response add(@RequestBody @Valid GoodsCollectRecordAddParam param, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
        }
        GoodsCollectRecord entity = new GoodsCollectRecord();
        BeanUtils.copyProperties(param, entity);
        // TODO 只接受指定参数
        return goodsCollectRecordService.insert(entity);
    }

    @ApiOperation(value = "删除用户商品收藏记录", notes = "")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Response delete(@RequestParam String id) {
        if (StringUtils.isBlank(id)) {
            return Response.failed("id不能为空");
        }
        return goodsCollectRecordService.deleteDataById(id);
    }

    @ApiOperation(value = "获取用户商品收藏记录详情", notes = "")
    @RequestMapping(value = "/getDetail", method = RequestMethod.GET)
    public Response<GoodsCollectRecordDetailVO> getDetail(@RequestParam String id) {
        if (StringUtils.isBlank(id)) {
            return Response.failed("id不能为空");
        }
        return goodsCollectRecordService.getDetail(id);
    }

    @ApiOperation(value = "获取用户商品收藏记录列表", notes = "")
    @RequestMapping(value = "getList", method = RequestMethod.GET)
    public Response<List<GoodsCollectRecordVO>> getList(@RequestBody @Valid GoodsCollectRecordQueryParam param,
            BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
        }
        QueryWrapper<GoodsCollectRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(param.getId() != null, GoodsCollectRecord::getId, param.getId())
                .eq(param.getUserId() != null, GoodsCollectRecord::getUserId, param.getUserId())
                .eq(param.getChannel() != null, GoodsCollectRecord::getChannel, param.getChannel())
                .eq(param.getStoreId() != null, GoodsCollectRecord::getStoreId, param.getStoreId())
                .eq(param.getSpuId() != null, GoodsCollectRecord::getSpuId, param.getSpuId())
                .eq(param.getSkuId() != null, GoodsCollectRecord::getSkuId, param.getSkuId())
                .eq(param.getDeleted() != null, GoodsCollectRecord::getDeleted, param.getDeleted());
        String key = "GoodsCollectRecordQueryParam{" + param.toString() + "}, Page{pageNumber=" + param.getPageNumber()
                + ", pageSize=" + param.getPageSize() + "}";
        return goodsCollectRecordService.getList(queryWrapper, key);
    }

    @ApiOperation(value = "分页查询用户商品收藏记录", notes = "")
    @RequestMapping(value = "/getPage", method = RequestMethod.GET)
    public Response<IPage<GoodsCollectRecordVO>> getPage(@RequestBody @Valid GoodsCollectRecordQueryParam param,
            BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
        }
        QueryWrapper<GoodsCollectRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(param.getId() != null, GoodsCollectRecord::getId, param.getId())
                .eq(param.getUserId() != null, GoodsCollectRecord::getUserId, param.getUserId())
                .eq(param.getChannel() != null, GoodsCollectRecord::getChannel, param.getChannel())
                .eq(param.getStoreId() != null, GoodsCollectRecord::getStoreId, param.getStoreId())
                .eq(param.getSpuId() != null, GoodsCollectRecord::getSpuId, param.getSpuId())
                .eq(param.getSkuId() != null, GoodsCollectRecord::getSkuId, param.getSkuId())
                .eq(param.getDeleted() != null, GoodsCollectRecord::getDeleted, param.getDeleted());
        IPage<GoodsCollectRecord> page = new Page<>(Long.parseLong(param.getPageNumber()),
                Long.parseLong(param.getPageSize()));
        String key =
                "GoodsCollectRecordQueryParamToPage{" + param.toString() + "}, Page{pageNumber=" + param.getPageNumber()
                        + ", pageSize=" + param.getPageSize() + "}";
        return goodsCollectRecordService.getPage(page, queryWrapper, key);
    }
}
